package cn.spike.product.service.impl;

import cn.spike.basic.service.IBaiduAiAuditService;
import cn.spike.basic.util.AjaxResult;
import cn.spike.product.domain.Product;
import cn.spike.product.domain.ProductDetail;
import cn.spike.product.domain.ProductOnlineAuditLog;
import cn.spike.product.mapper.ProductDetailMapper;
import cn.spike.product.mapper.ProductMapper;
import cn.spike.product.mapper.ProductOnlineAuditLogMapper;
import cn.spike.product.service.IProductService;
import cn.spike.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Override
    public void add(Product product) {
        super.add(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.add(productDetail);
    }

    @Override
    public void delete(Long id) {
        productDetailMapper.deleteByProductId(id);
        super.delete(id);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.updateByProductId(productDetail);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 上架业务
     * @param ids - 选中行的id
     * @return
     */
    @Override
    public AjaxResult onsale(List<Long> ids) {
        ProductOnlineAuditLog log = new ProductOnlineAuditLog();
        //上架审核 并添加审核日志
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            if(boo1 && boo2){//审核通过 - 添加日志 - 更改产品状态
                //上架
                productMapper.onsale(id,new Date());
                log.setNote("审核通过!!");
                log.setState(1);
                log.setProductId(id);
                productOnlineAuditLogMapper.add(log);
            }else {
                log.setNote("审核失败!!有违规内容");
                log.setState(0);
                log.setProductId(id);
                productOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMsg(product.getName() + "审核失败");
            }
        }
        return new AjaxResult();
    }

    /**
     * 下架业务
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public AjaxResult offsale(List<Long> ids) {
        //下架不用添加日志
        productMapper.offsale(ids,new Date());
        return new AjaxResult();
    }
}
